package com.googlecode.spruce.plugins.core.exec;

import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.Executor;

import com.googlecode.spruce.core.DefaultPluginResult;
import com.googlecode.spruce.core.ServiceCheckPlugin;
import com.googlecode.spruce.core.ServiceCheckPluginResult;

public class ExecCheckPlugin implements ServiceCheckPlugin
{

    private String command;

    private int exitValue;

    @Override
    public ServiceCheckPluginResult execute()
    {

        DefaultPluginResult result = null;
        final CommandLine cmdLine = CommandLine.parse(this.command);
        final Executor executor = new DefaultExecutor();
        executor.setExitValue(this.exitValue);

        try
        {
            final int exitCode = executor.execute(cmdLine);
            result = new DefaultPluginResult(exitCode);
        }
        catch (final Exception e)
        {
            result = new DefaultPluginResult(e);
        }

        return result;
    }

    public String getCommand()
    {
        return command;
    }

    public void setCommand(final String command)
    {
        this.command = command;
    }

    public int getExitValue()
    {
        return exitValue;
    }

    public void setExitValue(final int exitValue)
    {
        this.exitValue = exitValue;
    }

}
